home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 301-325 / disk_319 / cnewssrc / cnews.orig.lzh / rna / active.c next >
C/C++ Source or Header  |  1989-06-27  |  3KB  |  183 lines

  1. /*
  2.  * active file handling routines
  3.  *
  4.  * format of file:
  5.  *    <groupname> ' ' <5 digit #> ' ' <5 digit #> ' ' flag '\n'
  6.  *              (seq)          (low)
  7.  */
  8.  
  9. #include "defs.h"
  10.  
  11. static char actname[]     = ACTIVE;
  12. static int lineno;
  13. static active    *alist;
  14.  
  15. /*
  16.  * getseq - Get next sequence number for this group
  17.  *        and update active file.
  18.  *        If group missing append to file.
  19.  */
  20. char *
  21. getseq(group)
  22. char *group;
  23. {
  24.     register FILE    *f;
  25.     register int i;
  26.     char gbuf[BUFSIZ / 2], dbuf[BUFSIZ / 4], dbuf2[BUFSIZ / 4];
  27.     extern char *itoa();
  28.  
  29.     f = fopenl(actname);
  30.     lineno = 0;
  31.     while (getline(f, gbuf, dbuf, dbuf2))
  32.         if (CMP(gbuf, group) == 0) {
  33.             i = atoi(dbuf);
  34.             i++;
  35.             fseek(f, -12L, 1);
  36.             (void) fprintf(f, "%05d", i);
  37.             fclose(f);
  38. #if !AUSAM
  39.             unlock(actname);
  40. #endif
  41.             return itoa(i);
  42.         }
  43.     (void) fprintf(f, "%s 00001 00001 y\n", group);
  44.     fclose(f);
  45. #if !AUSAM
  46.     unlock(actname);
  47. #endif
  48.     return itoa(1);
  49. }
  50.  
  51.  
  52. static
  53. getline(f, g, d, d2)
  54. register FILE *f;
  55. char *g, *d, *d2;
  56. {
  57.     register int c;
  58.     register char *s;
  59.  
  60.     lineno++;
  61.     s = g;
  62.     while ((c = getc(f)) != ' ' && c != EOF)
  63.         *s++ = c;
  64.     *s = '\0';
  65.  
  66.     if (c != EOF) {
  67.         s = d;
  68.         while ((c = getc(f)) != EOF && isdigit(c))
  69.             *s++ = c;
  70.         *s = '\0';
  71.  
  72.         s = d2;
  73.         if (c == ' ')
  74.             while ((c = getc(f)) != EOF && isdigit(c))
  75.                 *s++ = c;
  76.         *s = '\0';
  77.  
  78.         if (c == ' ')
  79.             while ((c = getc(f)) != EOF && c != '\n')
  80.                 ;        /* eat flag */
  81.     }
  82.  
  83.     if (c != EOF && (c != '\n' || !*d || !*d2))
  84.         error("%s: bad format: line %d", actname, lineno);
  85.     return c != EOF;
  86. }
  87.  
  88.  
  89. /*
  90.  * build internal active file structure
  91.  */
  92. active *
  93. readactive()
  94. {
  95.     register FILE    *f;
  96.     register active    *ap, *last;
  97.     char gbuf[BUFSIZ / 2], dbuf[BUFSIZ / 4], dbuf2[BUFSIZ / 4];
  98.  
  99.     alist = last = NIL(active);
  100.     f = fopenf(actname, "r");
  101.     lineno = 0;
  102.     while (getline(f, gbuf, dbuf, dbuf2)) {
  103.         ap = NEW(active);
  104.         ap->a_name = newstr(gbuf);
  105.         ap->a_seq = atoi(dbuf);
  106.         ap->a_low = atoi(dbuf2);
  107.         ap->a_next = NIL(active);
  108.         if (!alist)
  109.             alist = ap;
  110.         else
  111.             last->a_next = ap;
  112.         last = ap;
  113.     }
  114.     fclose(f);
  115.     return alist;
  116. }
  117.  
  118.  
  119. /*
  120.  * return pointer to named group
  121.  */
  122. active *
  123. activep(grp)
  124. register char *grp;
  125. {
  126.     register active    *ap;
  127.  
  128.     for (ap = alist; ap; ap = ap->a_next)
  129.         if (CMP(grp, ap->a_name) == 0)
  130.             break;
  131.     return ap;
  132. }
  133.  
  134.  
  135. /*
  136.  * setlow - set the low number for this group
  137.  */
  138. setlow(group, low)
  139. char *group;
  140. int low;
  141. {
  142.     register FILE    *f;
  143.     char gbuf[BUFSIZ / 2], dbuf[BUFSIZ / 4], dbuf2[BUFSIZ / 4];
  144.     extern char *itoa();
  145.  
  146.     f = fopenl(actname);
  147.     lineno = 0;
  148.     while (getline(f, gbuf, dbuf, dbuf2))
  149.         if (CMP(gbuf, group) == 0) {
  150.             fseek(f, -6L, 1);
  151.             (void) fprintf(f, "%05d", low);
  152.             break;
  153.         }
  154.     fclose(f);
  155. #if !AUSAM
  156.     unlock(actname);
  157. #endif
  158. }
  159.  
  160.  
  161.  
  162. /*
  163.  * initgrp - initialise an entry for this group
  164.  */
  165. initgrp(group)
  166. char *group;
  167. {
  168.     register FILE    *f;
  169.     char gbuf[BUFSIZ / 2], dbuf[BUFSIZ / 4], dbuf2[BUFSIZ / 4];
  170.  
  171.     f = fopenl(actname);
  172.     lineno = 0;
  173.     while (getline(f, gbuf, dbuf, dbuf2))
  174.         if (CMP(gbuf, group) == 0) {
  175. #if !AUSAM
  176.             unlock(actname);
  177. #endif
  178.             return;
  179.         }
  180.     (void) fprintf(f, "%s 00000 00001\n", group);
  181.  
  182. }
  183.